package com.ruoyi.web.controller.tag;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tag.domain.TagsInfo;
import com.ruoyi.tag.service.ITagsInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 标签管理Controller
 *
 * @author Amia
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/tag/tag")
@Tag(name = "【标签管理】管理")
public class TagsInfoController extends BaseController {
    @Autowired
    private ITagsInfoService tagsInfoService;

    /**
     * 查询标签管理列表
     */
    @Operation(summary = "查询标签管理列表")
    @GetMapping("/list")
    public TableDataInfo list(TagsInfo tagsInfo) {
        startPage();
        List<TagsInfo> list = tagsInfoService.selectTagsInfoList(tagsInfo);
        return getDataTable(list);
    }

    /**
     * 导出标签管理列表
     */
    @Operation(summary = "导出标签管理列表")
    @Log(title = "标签管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TagsInfo tagsInfo) {
        List<TagsInfo> list = tagsInfoService.selectTagsInfoList(tagsInfo);
        ExcelUtil<TagsInfo> util = new ExcelUtil<TagsInfo>(TagsInfo.class);
        util.exportExcel(response, list, "标签管理数据");
    }

    /**
     * 获取标签管理详细信息
     */
    @Operation(summary = "获取标签管理详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tagsInfoService.selectTagsInfoById(id));
    }

    /**
     * 根据帖子Id获取标签管理详细信息
     */
    @Operation(summary = "根据帖子Id获取标签管理详细信息")
    @GetMapping(value = "/post/{postId}")
    public AjaxResult getInfoByPostId(@PathVariable("postId") Long postId) {
        List<TagsInfo> tagsInfoList = tagsInfoService.getInfoByPostId(postId);
        return success(tagsInfoList);
    }

    /**
     * 根据圈子Id获取标签管理详细信息
     */
    @Operation(summary = "根据圈子Id获取标签管理详细信息")
    @GetMapping(value = "/circle/{circleId}")
    public AjaxResult getInfoByCircleId(@PathVariable("circleId") Long circleId) {
        List<TagsInfo> tagsInfoList = tagsInfoService.getInfoByCircleId(circleId);
        return success(tagsInfoList);
    }

    /**
     * 获取标签分类
     */
    @Operation(summary = "获取标签分类")
    @GetMapping(value = "/category")
    public AjaxResult getTagCategory() {
        List<String> tagCategoryList = tagsInfoService.getTagCategory();
        return success(tagCategoryList);
    }

    /**
     * 新增标签管理
     */
    @Operation(summary = "新增标签管理")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TagsInfo tagsInfo) {
        tagsInfo.setCreatedBy(getUsername());
        int rows = tagsInfoService.insertTagsInfo(tagsInfo);
        if (rows > 0) {
            return AjaxResult.success("新增成功", tagsInfo.getId()); // 返回新增标签的ID
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改标签管理
     */
    @Operation(summary = "修改标签管理")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TagsInfo tagsInfo) {
        tagsInfo.setUpdatedBy(getUsername());
        return toAjax(tagsInfoService.updateTagsInfo(tagsInfo));
    }

    /**
     * 删除标签管理
     */
    @Operation(summary = "删除标签管理")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable(name = "ids") Long[] ids) {
        return toAjax(tagsInfoService.deleteTagsInfoByIds(ids));
    }

    /**
     * 查询全部标签管理列表
     */
    @Operation(summary = "查询全部标签管理列表")
    @GetMapping("/all")
    public TableDataInfo getAllTagsInfoList() {
        List<TagsInfo> list = tagsInfoService.getAllTagsInfoList();
        return getDataTable(list);
    }

}
